package steed.springboot;

import java.io.File;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;

import steed.util.UtilsUtil;
import steed.util.base.BaseUtil;
import steed.util.base.PathUtil;
import steed.util.base.PropertyUtil;

@Controller
//@EnableAutoConfiguration
@EnableAutoConfiguration(exclude={JtaAutoConfiguration.class})
@Configuration
@ServletComponentScan(basePackages = "steed")
public class App implements EmbeddedServletContainerCustomizer{
	
	/*@Bean
	public InMemoryUserDetailsManager inMemoryUserDetailsManager() {
		UserDetails user =  User.withUsername("ifs").password("password").roles("USER").build();
		return new InMemoryUserDetailsManager(Arrays.asList(user));
	}*/

	public static void main(String[] args) throws Exception {
		new UtilsUtil().initUtils();
//		RuntimeMXBean rmb=(RuntimeMXBean)ManagementFactory.getRuntimeMXBean();  
//		BaseUtil.getLogger().debug("getClassPath "+rmb.getClassPath());  
//		System.out.println("getLibraryPath "+rmb.getLibraryPath());  
//		System.out.println("getVmVersion "+rmb.getVmVersion());  		
//		System.out.println("getBootClassPath "+rmb.getBootClassPath());  
//		TomcatEmbeddedServletContainerFactory tomcat=new TomcatEmbeddedServletContainerFactory();
//		tomcat.setUriEncoding(Charset.forName("UTF-8"));
//		tomcat.set
		RedisAutoConfiguration autoConfiguration;
		SpringApplication.run(App.class, args);
	}

	@Override
	public void customize(ConfigurableEmbeddedServletContainer container) {
		//自定义端口号
		//container.setPort(8088);
//		container.
		String praseRelativePath = "webapp/";
		praseRelativePath = PathUtil.praseRelativePath(PropertyUtil.getConfig("tomcat.documentRoot"));
		if (new File(praseRelativePath).exists()) {
			BaseUtil.getLogger().debug("设置内嵌tomcat的documentRoot为{}",praseRelativePath);
			container.setDocumentRoot(new File(praseRelativePath));
		}else {
			BaseUtil.getLogger().debug("内嵌tomcat的documentRoot:{}未找到!",praseRelativePath);
		}
	}
	
	@Bean
	public EmbeddedServletContainerCustomizer containerCustomizer() {

	   return (container -> {
	        container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/WEB-INF/jsp/404.jsp"),
	        		new ErrorPage(HttpStatus.UNAUTHORIZED, "/WEB-INF/jsp/404.jsp"), 
	        		new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/WEB-INF/jsp/404.jsp"));
	   });
	}
}
